<template>
  <div class="container" @viewappear="onViewAppear">
    <navbar title="设置" theme="light"></navbar>
    <list>
      <page-refresh v-on:refresh="onXRefresh" v-on:pullingDown="onPullingDown" :refreshing="refreshing"
                    :indicatorScale="indicatorScale"></page-refresh>
      <cell>
        <div class="panel">
          <wxc-cell title="手机号"
                    :has-arrow="true"
                    @wxcCellClicked="bindPhone">
            <text slot="value" class="cell-value">{{info.phone}}</text>
          </wxc-cell>
          <wxc-cell title="昵称"
                    :has-arrow="true"
                    @wxcCellClicked="editNickname">
            <text slot="value" class="cell-value">{{info.nickname}}</text>
          </wxc-cell>
          <wxc-cell title="淘宝授权"
                    v-if="iosShow"
                    :has-arrow="true"
                    @wxcCellClicked="bindTaobao">
            <text slot="value" class="cell-value">{{info.taobaoSpecialId ? '已授权' : '未授权'}}</text>
          </wxc-cell>
          <wxc-cell title="微信"
                    v-if="iosShow"
                    :has-arrow="true"
                    @wxcCellClicked="bindWeixin">
            <text slot="value" class="cell-value">{{info.weixinBind ? '已绑定' : '未绑定'}}</text>
          </wxc-cell>
          <wxc-cell title="地区"
                    v-if="iosShow"
                    :has-arrow="true"
                    @wxcCellClicked="editLocation">
            <text slot="value" class="cell-value">{{info.city + info.district}}</text>
          </wxc-cell>
        </div>
        <div class="panel">
          <wxc-cell title="修改密码"
                    :has-arrow="true"
                    @wxcCellClicked="modifyPwd">
          </wxc-cell>
        </div>
      </cell>
    </list>
    <text class="logout-btn activeable" @click="exit">退出登录</text>
    <wxc-dialog title="温馨提示"
                content="确认退出登录吗？"
                :show="logoutShow"
                :single="false"
                @wxcDialogCancelBtnClicked="cancelLogout"
                @wxcDialogConfirmBtnClicked="doLogout">
    </wxc-dialog>
    <wxc-dialog title="温馨提示"
                :content="'您' + (info.weixinBind ? '已经绑定微信' : '还未绑定微信') + '，通过微信可以实现快捷登录！'"
                :show="bindWeixinShow"
                :single="false"
                :confirm-text="info.weixinBind ? '重新绑定' : '现在绑定'"
                @wxcDialogCancelBtnClicked="cancelWeixinBind"
                @wxcDialogConfirmBtnClicked="doWeixinBind">
    </wxc-dialog>
    <wxc-loading :show="isLoadingShow" ></wxc-loading>
  </div>
</template>
<style lang="scss">
  @import "./sass/var";
  @import "./sass/mixin";
  @import "./sass/utilities";
  @import "./sass/icon";
  @import "./sass/activeable";
  .container{
    background-color: $body-bg;
    transition: transform .3s linear
  }

  .panel {
    margin-bottom: 10px;
    margin-top: 10px;
    &-body {
      padding: 20px;
    }
  }

  .cell-title {
    flex-direction: row;
    align-items: center;
    &-icon {
      margin-right: 10px;
      font-size: 40px;
      font-weight: 300;
    }
    &-text {
      color: $color-muted;
      font-size: 32px;
      font-weight: 300;
    }
  }
  .cell-value {
    color: $color;
    font-size: 28px;
    font-weight: 300;
    flex: 1;
    text-align: right;
    height: 40px;
    line-height: 40px;
  }

  .logout-btn {
    height: 100px;
    line-height: 100px;
    background-image: $gradient-bg-primary;
    color: #ffffff;
    font-size: 36px;
    text-align: center;
  }

</style>
<script>

import {WxcCell, WxcDialog, WxcLoading} from 'weex-ui'
let api = require('./include/api')
let taobaoAuth = require('./include/taobao-auth')
let modal = weex.requireModule('modal')
let navigator = require('./include/xnavigator')
let login = require('./include/login')
let BroadcastChannel = require('./include/broadcast-channel')
let broadcastChannelKeys = require('./include/broadcast-channel-keys')
let wexinAuthBroadcaseChannel = new BroadcastChannel(broadcastChannelKeys.WEIXIN_AUTH)
let loginBroadcastChannel = new BroadcastChannel(broadcastChannelKeys.LOGIN)
require('./include/page-xdom')
let config = require('./include/app-config')
module.exports = {
  mixins: [
    require('./mixin/page-refresh')
  ],
  components: {
    WxcCell,
    WxcDialog,
    WxcLoading,
    navbar: require('./components/navbar.vue')
  },
  data () {
    return {
      logoutShow: false,
      info: {},
      bindWeixinShow: false,
      isLoadingShow: false,
      iosShow: false,
      loginDoing: false
    }
  },
  created () {
    config.bindConfig(this)
    this.loadData()
    loginBroadcastChannel.onmessage = (e) => {
      this.loginDoing = false
      if (e.data) {
        this.loadData()
      } else {
      }
    }
    wexinAuthBroadcaseChannel.onmessage = e => {
      if (e.data.state === 'app_bind') {
        // 微信确认登录，现在开始微信登录
        this.doWeixinBind(e, e.data.code)
      }
    }
  },
  methods: {
    onViewAppear () {
      this.loadData()
    },
    loadData () {
      return api.page.settings.data().done(rs => {
        let data = rs.data
        this.info = data
      })
    },
    exit () {
      this.logoutShow = true
    },
    cancelLogout () {
      this.logoutShow = false
    },
    doLogout () {
      this.logoutShow = false
      login.invalidate().done(rs => {
        api.logout().done(rs => {
          navigator.pop()
        })
      })
    },
    onXRefresh (event) {
      this.onRefresh(event, this.doRefresh())
    },
    doRefresh () {
      return this.loadData()
    },
    bindPhone () {
      navigator.push('bind-phone.js')
    },
    editNickname (e) {
      navigator.push('user-edit.js?title=' + encodeURIComponent('修改昵称') + '&value=' + encodeURIComponent(this.info.nickname) + '&field=nickname')
    },
    editLocation () {
      navigator.push('my-location.js')
    },
    citySelected (e) {
      modal.alert({message: JSON.stringify(e)})
      api.user.updateField({
        field: 'city',
        value: e.item.cityName
      }).done(rs => {
        this.info.city = e.item.cityName
      })
    },
    modifyPwd () {
      navigator.push('modify-pwd.js')
    },
    bindWeixin () {
      this.bindWeixinShow = true
    },
    cancelWeixinBind () {
      this.bindWeixinShow = false
    },
    bindTaobao () {
      taobaoAuth.auth().done(() => {
        this.doRefresh()
      })
    },
    doWeixinBind (e, code) {
      if (code) {
        this.isLoadingShow = true
        api.user.bindWeixin({weixinAuthCode: code}).done(rs => {
          modal.toast({message: '微信绑定成功', duration: 0.5})
          this.info.weixinBind = true
        }).always(rs => {
          this.isLoadingShow = false
        })
      } else {
        let weixinApi = weex.requireModule('weixinApi')
        weixinApi.sendAuth({state: 'app_bind'})
        this.bindWeixinShow = false
      }
    }
  }
}
</script>
